package com.iframework.anson;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.iframework.model.json.Female;
import com.iframework.model.json.MainRanking;
import com.iframework.model.json.Male;
import com.iframework.model.json.Ranking;
import com.iframework.model.json.Rankings;
import com.iframework.tools.ApiUtil;
import com.iframework.tools.BaseTools;
import com.iframework.tools.DBHelp_JDBC_MySQL;

public class MainRankings {

	public MainRankings(boolean isThread){
		Gson gson=new Gson();
		String url="http://api.zhuishushenqi.com/ranking/gender";//获取排行榜列表1
		String mainRankingJSON=ApiUtil.send(url);
        MainRanking mainRanking=gson.fromJson(mainRankingJSON, MainRanking.class);
        DBHelp_JDBC_MySQL db=new DBHelp_JDBC_MySQL(isThread);
        saveRankings(gson, mainRanking,db,isThread);
        
        
        //BaseTools.log(DBHelp_JDBC_MySQL.getCount("tbl_MainRanking").toString());
	}
	private static void saveRankings(Gson gson, MainRanking mainRanking,DBHelp_JDBC_MySQL db, boolean isThread) {
		// 表名
		String tabName = "tbl_MainRanking";
		// 表字段
		String[] tab_fields = { "_id", "title", "cover", "collapse", "monthRank", "totalRank", "gender", "tag", "isSub",
				"isNew", "priority", "_v", "created" };
		String[] u_fields = { "title", "cover", "collapse", "monthRank", "totalRank", "gender", "tag", "isSub", "isNew",
				"priority", "_v", "updated" };
		
		String[] q_fields = { "_id" };
		String strNowTime = BaseTools.getNow();
		String url;
		Rankings rankings;
		Ranking ranking;
		String rankingsJSON;

		// 男生榜
		List<Male> males = mainRanking.getMale();
		while (males.iterator().hasNext()) {
			Male male = males.get(0);
			url = "http://api.zhuishushenqi.com/ranking/" + male.get_id();// 根据排行榜id获得某个排行榜详情
			rankingsJSON = ApiUtil.send(url).replace("\"new\"", "\"isNew\"");
			rankings = gson.fromJson(rankingsJSON, Rankings.class);
			ranking = rankings.getRanking();
			String[] q_data = { male.get_id() };
			String[] q_result = DBHelp_JDBC_MySQL.query(tabName, q_fields, q_data, q_fields);
			if (q_result == null) {
				males.remove(male);
				continue;
			}
			if (q_result.length == 1 && q_result[0] == null) {
				BaseTools.log(male.getTitle());
				String[] data = { male.get_id().toString(), male.getTitle(), male.getCover(),
						String.valueOf(male.getCollapse()), male.getMonthRank(), male.getTotalRank(),
						ranking.getGender(), ranking.getTag(), String.valueOf(ranking.getIsSub()),
						String.valueOf(ranking.getIsNew()), String.valueOf(ranking.getPriority()),
						String.valueOf(ranking.get__v()), strNowTime };
				DBHelp_JDBC_MySQL.insert(tabName, tab_fields, data,male.getTitle());
			} else {
				String[] u_data = { male.getTitle(), male.getCover(), String.valueOf(male.getCollapse()),
						male.getMonthRank(), male.getTotalRank(), ranking.getGender(), ranking.getTag(),
						String.valueOf(ranking.getIsSub()), String.valueOf(ranking.getIsNew()),
						String.valueOf(ranking.getPriority()), String.valueOf(ranking.get__v()), strNowTime };
				DBHelp_JDBC_MySQL.update(tabName, u_fields, u_data, q_fields, q_data,male.getTitle());
			}
			if (isThread) {
				// 多线程并发
				new Thread(new BookManagerTheard(db,
						DBHelp_JDBC_MySQL.query(tabName, q_fields, q_data, new String[] { "id" })[0],
						ranking.getBooks())).start();
				BaseTools.log("多线程并发");
			} else {
				// 流模式
				BookManager.BookManager(DBHelp_JDBC_MySQL.query(tabName, q_fields, q_data, new String[] { "id" })[0],
						ranking.getBooks());
				BaseTools.log("流模式");
			}
			males.remove(male);
		}
		

		// 女生榜
		List<Female> females = mainRanking.getFemale();
		while (females.iterator().hasNext()) {
			Female female = females.get(0);
			url = "http://api.zhuishushenqi.com/ranking/" + female.get_id();// 根据排行榜id获得某个排行榜详情
			rankingsJSON = ApiUtil.send(url).replace("\"new\"", "\"isNew\"");
			rankings = gson.fromJson(rankingsJSON, Rankings.class);
			ranking = rankings.getRanking();
			String[] q_data = { female.get_id() };
			String[] q_result = DBHelp_JDBC_MySQL.query(tabName, q_fields, q_data, q_fields);
			if (q_result == null) {
				females.remove(female);
				continue;
			}
			if (q_result.length == 1 && q_result[0] == null) {
				BaseTools.log(female.getTitle());
				String[] data = { female.get_id().toString(), female.getTitle(), female.getCover(),
						String.valueOf(female.getCollapse()), female.getMonthRank(), female.getTotalRank(),
						ranking.getGender(), ranking.getTag(), String.valueOf(ranking.getIsSub()),
						String.valueOf(ranking.getIsNew()), String.valueOf(ranking.getPriority()),
						String.valueOf(ranking.get__v()), strNowTime };
				DBHelp_JDBC_MySQL.insert(tabName, tab_fields, data,female.getTitle());
			} else {
				String[] u_data = { female.getTitle(), female.getCover(), String.valueOf(female.getCollapse()),
						female.getMonthRank(), female.getTotalRank(), ranking.getGender(), ranking.getTag(),
						String.valueOf(ranking.getIsSub()), String.valueOf(ranking.getIsNew()),
						String.valueOf(ranking.getPriority()), String.valueOf(ranking.get__v()), strNowTime };
				DBHelp_JDBC_MySQL.update(tabName, u_fields, u_data, q_fields, q_data,female.getTitle());
			}
			if (isThread) {
				// 多线程并发
				new Thread(new BookManagerTheard(db,
						DBHelp_JDBC_MySQL.query(tabName, q_fields, q_data, new String[] { "id" })[0],
						ranking.getBooks())).start();
			} else {
				// 流模式
				BookManager.BookManager(DBHelp_JDBC_MySQL.query(tabName, q_fields, q_data, new String[] { "id" })[0],
						ranking.getBooks());
			}
			females.remove(female);

		}
		if (isThread) {
			db.close();
		}
	}
}
